/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luedinski.objects;

import java.text.DecimalFormat;

/**
 * 
 * @author lueder
 */
public final class Time {

	private int minutes;

	public Time() {
	}

	public Time(int minutes) {
		setMinutes(minutes);
	}

	public Time(int hours, int minutes) {
		setTime(hours, minutes);
	}

	public int getMinutes() {
		return minutes;
	}

	public void setMinutes(int minutes) {
		this.minutes = (minutes % 1440 < 0) ? 1440 + minutes % 1440
				: minutes % 1440;
	}

	public String getTime() {
		DecimalFormat f = new DecimalFormat("00");
		return f.format(minutes / 60) + ":" + f.format(minutes % 60);
	}

	public void setTime(int hours, int minutes) {
		if (hours > 23 || minutes > 59) {
			throw new NumberFormatException("time out of bounds");
		} else {
			setMinutes(hours * 60 + minutes);
		}
	}

	public void addMinutes(int minutes) {
		setMinutes(this.minutes + minutes);
	}

	public void addTime(int hours, int minutes) {
		setMinutes(hours * 60 + minutes + this.minutes);
	}

	/**
	 * This - Objekt Minuten
	 * 
	 * @param t
	 *            Time
	 * @return Differenz
	 */
	public int compareTo(Time t) {
		return this.minutes - t.getMinutes();
	}

	public static Time timefromStringInput(String input) {
		if (input.matches("[0-9]{2}:[0-9]{2}")) {
			String[] timeArray = input.split(":");
			return new Time(Integer.parseInt(timeArray[0]), Integer.parseInt(timeArray[1]));
		} else
			return null;
	}
}
